package top.infopub.mgr.sg.service.impl;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.enums.sg.CoopInfoTypesEnums;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.sg.dao.CoopInfoDao;
import top.infopub.mgr.sg.dao.CoopInfoLogDao;
import top.infopub.mgr.sg.service.CoopInfoService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.sg.CoopInfo;
import top.infopub.model.sg.CoopInfoLog;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class CoopInfoServiceImpl extends GenericServiceImpl<CoopInfo, String> implements CoopInfoService {

    @Autowired
    private CoopInfoDao coopInfoDao;

    @Autowired
    private CoopInfoLogDao coopInfoLogDao;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Override
    public GenericDao<CoopInfo, String> getDao() {
        return coopInfoDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCoopInfo(CoopInfo coopInfo) {
        // 执行新增
        if (StringUtils.isEmpty(coopInfo.getId())) {
            String coopCode = "sg_coop_info";
            String generatorCode = codeGeneratorConfigService.generatorCode(coopCode);
            coopInfo.setCoopCode(generatorCode);
            coopInfo.setState(1);//默认是1=可用
            coopInfo.setAddBy(SessionUserUtils.getSessionUserId());
            coopInfo.setAddTime(DateUtils.getCurrentDateTime());
            coopInfo.setUptBy(SessionUserUtils.getSessionUserId());
            coopInfo.setUptTime(DateUtils.getCurrentDateTime());
            coopInfoDao.insert(coopInfo);

            CoopInfoLog coopInfoLog = new CoopInfoLog();
            coopInfoLog.setCoopInfoId(coopInfo.getId());
            coopInfoLog.setSaveBy(SessionUserUtils.getSessionUserId());
            coopInfoLog.setSaveName(SessionUserUtils.getSessionAttributeForUserDtl().getRealName());
            coopInfoLog.setSaveTime(DateUtils.getCurrentDateTime());
            coopInfoLogDao.insert(coopInfoLog);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void udpCoopInfo(CoopInfo coopInfo) {
        //执行修改
        CoopInfo udpCoopInfo = coopInfoDao.selectByPrimaryKey(coopInfo.getId());
        coopInfo.setCoopCode(udpCoopInfo.getCoopCode());
        coopInfo.setState(udpCoopInfo.getState());
        coopInfo.setAddBy(udpCoopInfo.getAddBy());
        coopInfo.setAddTime(udpCoopInfo.getAddTime());
        coopInfo.setUptBy(SessionUserUtils.getSessionUserId());
        coopInfo.setUptTime(DateUtils.getCurrentDateTime());
        BeanUtils.copyProperties(coopInfo, udpCoopInfo);
        this.updateByPrimaryKey(udpCoopInfo);

        CoopInfoLog coopInfoLog = new CoopInfoLog();
        coopInfoLog.setCoopInfoId(coopInfo.getId());
        coopInfoLog.setSaveBy(SessionUserUtils.getSessionUserId());
        coopInfoLog.setSaveName(SessionUserUtils.getSessionAttributeForUserDtl().getRealName());
        coopInfoLog.setSaveTime(DateUtils.getCurrentDateTime());
        coopInfoLogDao.insert(coopInfoLog);
    }

    @Override
    public boolean selectByCoopName(String coopName) {
        CoopInfo coopInfo = coopInfoDao.selectByCoopName(coopName);
        if (null != coopInfo) {
            return true;
        }
        return false;
    }

    @Override
    public boolean selectByCoopNameAndId(String coopName, String id) {
        CoopInfo coopInfo = coopInfoDao.selectByCoopNameAndId(coopName, id);
        if (null != coopInfo) {
            return true;
        }
        return false;
    }

    @Override
    public Page<CoopInfo> selectPageByDomainWhere(Page<CoopInfo> page, CoopInfo coopInfo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<CoopInfo>)coopInfoDao.selectByDomainWhere(coopInfo);
    }

    @Override
    public List<CoopInfo> selectByCoopCode(String coopId) {
        return coopInfoDao.selectByCoopCode(coopId);
    }

    @Override
    public List<CoopInfo> selectAll(String zbtypes,String fbtypes,String qttypes) {
        return coopInfoDao.selectAll(zbtypes,fbtypes,qttypes);
    }

    @Override
    public List<CoopInfo> queryByTypes(CoopInfoTypesEnums coopInfoTypes, int flag) {
        return coopInfoDao.queryByIsIncludeTypes(flag, coopInfoTypes.getTypes());
    }
}